package org.example;

import org.example.protobuf.Example.Person;
import org.example.protobuf.Example.Person.PhoneNumber;
import org.example.protobuf.Example.Person.PhoneType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

public class ProtobufDataGenerator {
    private static final int RECORD_COUNT = 1000;
    private static final String OUTPUT_FILE = "persons_data.bin";
    private static final Random RANDOM = new Random();

    // 预定义的姓名列表
    private static final String[] FIRST_NAMES = {
            "张", "王", "李", "赵", "刘", "陈", "杨", "黄", "周", "吴",
            "徐", "孙", "马", "朱", "胡", "林", "郭", "何", "高", "罗"
    };

    private static final String[] LAST_NAMES = {
            "伟", "芳", "娜", "秀英", "敏", "静", "强", "磊", "军", "洋",
            "勇", "艳", "杰", "娟", "涛", "明", "超", "秀兰", "霞", "婷"
    };

    // 预定义的域名列表
    private static final String[] DOMAINS = {
            "gmail.com", "yahoo.com", "hotmail.com", "outlook.com",
            "qq.com", "163.com", "126.com", "sina.com", "sohu.com"
    };

    public static void main(String[] args) {
        try (FileOutputStream fos = new FileOutputStream(OUTPUT_FILE)) {
            long startTime = System.currentTimeMillis();

            // 批量生成并写入1000条记录
            for (int i = 0; i < RECORD_COUNT; i++) {
                Person person = generateRandomPerson(i);
                person.writeDelimitedTo(fos);
            }

            long endTime = System.currentTimeMillis();
            System.out.println("成功生成 " + RECORD_COUNT + " 条记录到文件: " + OUTPUT_FILE);
            System.out.println("耗时: " + (endTime - startTime) + " 毫秒");

        } catch (IOException e) {
            System.err.println("写入文件时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static Person generateRandomPerson(int id) {
        // 生成随机姓名
        String firstName = FIRST_NAMES[RANDOM.nextInt(FIRST_NAMES.length)];
        String lastName = LAST_NAMES[RANDOM.nextInt(LAST_NAMES.length)];
        String fullName = firstName + lastName;

        // 生成随机邮箱
        String email = fullName + "@" + DOMAINS[RANDOM.nextInt(DOMAINS.length)];
        email = email.toLowerCase().replaceAll("[^a-z0-9@.]", "");

        // 生成随机电话号码
        PhoneNumber mobilePhone = generatePhoneNumber(PhoneType.MOBILE);
        PhoneNumber homePhone = generatePhoneNumber(PhoneType.HOME);

        return Person.newBuilder()
                .setId(id)
                .setName(fullName)
                .setEmail(email)
                .addPhones(mobilePhone)
                .addPhones(homePhone)
                .build();
    }

    private static PhoneNumber generatePhoneNumber(PhoneType type) {
        // 生成随机11位电话号码
        StringBuilder sb = new StringBuilder();
        sb.append("1");
        for (int i = 0; i < 10; i++) {
            sb.append(RANDOM.nextInt(10));
        }

        return PhoneNumber.newBuilder()
                .setNumber(sb.toString())
                .setType(type)
                .build();
    }
}
